#define _CRT_SECURE_NO_WARNINGS 1

#include"List.h"

void LTPrint(LTNode* phead)
{
	assert(phead);
	LTNode* ptmp = phead->next;
	while (ptmp != phead)
	{
		printf("%d->", ptmp->date);
		ptmp = ptmp->next;
	}
	printf("\n");
}
LTNode* LTBuyNode(LTDateType x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	if (newnode == NULL)
	{
		perror("malloc fail !");
		exit(1);
	}
	newnode->date = x;
	newnode->next = newnode;
	newnode->prev = newnode;
	return newnode;
}
LTNode* LTInit()
{
	return LTBuyNode(-1);
}




void LTPushBack(LTNode* phead, LTDateType x)
{
	assert(phead);
	LTNode* newnode = LTBuyNode(x);
	newnode->next = phead;
	newnode->prev = phead->prev;

	phead->prev->next = newnode;
	phead->prev = newnode;
}

void LTPushFront(LTNode* phead, LTDateType x)
{
	assert(phead);
	LTNode* newnode = LTBuyNode(x);
	newnode->next = phead->next;
	newnode->prev = phead;

	phead->next->prev = newnode;
	phead->next = newnode;
}

void LTPopBack(LTNode* phead)
{
	assert(phead);
	assert(phead->prev != phead);

	LTNode* ptmp = phead->prev;
	ptmp->prev->next = phead;
	phead->prev = ptmp->prev;
	free(ptmp);
	ptmp = NULL;
}
void LTPopFront(LTNode* phead)
{
	assert(phead);
	assert(phead->prev != phead);
	LTNode* ptmp = phead->next;
	ptmp->next->prev = phead;
	phead->next = ptmp->next;
	free(ptmp);
	ptmp = NULL;
}

void LTInsert(LTNode* pos, LTDateType x)
{
	assert(pos);

	LTNode* newnode = LTBuyNode(x);
	newnode->next = pos->next;
	newnode->prev = pos;
	pos->next->prev = newnode;
	pos->next = newnode;
}

void LTErase(LTNode* pos)
{
	assert(pos);
	assert(pos->next != pos);

	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;
}
LTNode* LTFind(LTNode* phead, LTDateType x)
{
	assert(phead);
	assert(phead->next != phead);

	LTNode* ptmp = phead->next;
	while (ptmp != phead)
	{
		if (ptmp->date == x)
		{
			return ptmp;
		}
		ptmp = ptmp->next;
	}
	printf("NOT FIND\n");
	return NULL;
}

void LTDesTory(LTNode** pphead)
{
	assert(pphead);
	assert(*pphead);
	LTNode* ptmp = (*pphead)->next;
	while (ptmp != *pphead)
	{
		LTNode* next = ptmp->next;
		free(ptmp);
		ptmp = next;
	}
	free(ptmp);
	ptmp = NULL;
}

void LTDesTory2(LTNode* phead)
{
	assert(phead);
	assert(phead);
	LTNode* ptmp = phead->next;
	while (ptmp != phead)
	{
		LTNode* next = ptmp->next;
		free(ptmp);
		ptmp = next;
	}
	free(ptmp);
	ptmp = NULL;
}
